layui.use(['layer','form','jquery','laydate','upload','element'],function () {
    var form = layui.form;
    var layer = layui.layer;
    var $ = layui.$;
    var laydate = layui.laydate;
    var upload = layui.upload;
    var element = layui.element;
    //开始时间 结束时间渲染
    laydate.render({
        elem: '#expireDate',
        type: 'datetime'
    });
    laydate.render({
        elem: '#startDate',
        type: 'datetime'
    });
    //等父页面轮播图id赋值后在加载
    setTimeout(getCurrentBannerMessage,200);
    //根据id查询轮播图
    function getCurrentBannerMessage() {
        var carouselId = $('#carouselId').val();
        $.ajax({
            url: '/banner/getBannerMessageById',
            data: {carouselId: carouselId},
            dataType: 'json',
            success: function (data) {
                var banner = data.data;
                $('#headPortrait').attr('src',banner.imgAddress);
                $('#imgTitle').val(banner.imgTitle)
                $('#startDate').val(banner.startDate)
                $('#expireDate').val(banner.expireDate)
                $('#describe').val(banner.describe)
                $('#remarks').val(banner.remarks)
                $('#bannerViedo').attr('src',banner.videoAddress);
                $('#display').val(banner.display);
                $('#imgType').val(banner.imgType);
                $('#videoAddress').val(banner.videoAddress);
                $('#oldVideoAddress').val(banner.videoAddress);
                $('#imgAddress').val(banner.imgAddress);
                $('#oldImgAddress').val(banner.imgAddress);
                form.render();
            }
        });
    }
    var imgFiles;
    var imgIndex;
    var imgUploadCount = 0;
    //修改图片
    upload.render({
        elem: '#headPortrait',
        accept: 'images',
        size: 2048,
        field: 'image',
        url: '/banner/uploadBannerImage',
        choose: function(obj){
            imgUploadCount++;
            obj.preview(function(index, file, result){
                $('#headPortrait').attr('src',result);
                imgIndex = index;
            });
            if(imgUploadCount != 1){
                delete imgFiles[imgIndex];
            }
            imgFiles = obj.pushFile();
            element.progress('imgProgress', '99%');
        },
        done: function (res,index,upload) {
            if(res.data.code == 200){
                $('#imgAddress').val(res.data.src);
                layer.msg('上传图片成功');
                element.progress('imgProgress', '100%');
            }
        }
    })

    var videoFiles;
    var videoIndex;
    var videoUploadCount = 0;
    //上传视频
    upload.render({
        elem: '#uploadBannerViedo',
        accept: 'video',
        size: 409600 ,
        field: 'video',
        url: '/banner/uploadBannerVideo',
        choose: function(obj){
            videoUploadCount++;
            obj.preview(function(index, file, result){
                videoIndex = index;
                $('#bannerViedo').attr('src',result);
            });
            if(videoUploadCount != 1){
                delete videoFiles[videoIndex];
            }
            videoFiles = obj.pushFile();
            element.progress('videoProgress', '99%');
        },
        done: function (res,index,upload) {
            if(res.data.code == 200){
                $('#videoAddress').val(res.data.src);
                layer.msg('上传视频成功');
                element.progress('videoProgress', '100%');
            }
        }
    })
    //修改按钮
    form.on('submit(sub)',function (obj) {
        $.ajax({
            url: '/banner/updateBannerMessage',
            type: 'post',
            dataType: 'json',
            data: obj.field,
            success: function (data) {
                if(data){
                    var layer = parent.layer;
                    layer.close(layer.index)
                }
            }
        })
        return false;
    })
    //关闭按钮
    $('#close').click(function () {
        var layer = parent.layer;
        layer.close(layer.index)
    });
    function getTime(){
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth()+1;
        var day = date.getDate();
        var hour = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();
        var dateStr = year+"-"+zeroFilling(month)+"-"+zeroFilling(day)+" "+zeroFilling(hour)+":"+zeroFilling(minute)+":"+zeroFilling(second);
        return dateStr;
    }
    //补零
    function zeroFilling(data){
        if(data<10){
            data="0"+data;
        }
        return data;
    }
})